package cn.stylefeng.guns.modular.weixin.handler;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.*;
import cn.stylefeng.guns.modular.system.service.*;
import org.springframework.beans.factory.annotation.*;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.common.session.*;
import me.chanjar.weixin.mp.bean.message.*;
import org.apache.commons.lang3.*;
import me.chanjar.weixin.mp.builder.outxml.*;
import cn.stylefeng.guns.modular.system.entity.*;
import java.util.stream.*;
import org.apache.commons.lang3.math.*;
import org.springframework.util.*;
import cn.hutool.core.util.*;
import org.springframework.util.ObjectUtils;

import java.util.regex.*;
import java.util.*;

@Component
public class MsgHandler extends AbstractHandler
{
    @Autowired
    private DictService dictService;
    
    public WxMpXmlOutMessage handle(final WxMpXmlMessage wxMessage, final Map<String, Object> context, final WxMpService weixinService, final WxSessionManager sessionManager) {
        if (StringUtils.equalsIgnoreCase((CharSequence)wxMessage.getEvent(), (CharSequence)"TEMPLATESENDJOBFINISH")) {
            return null;
        }
        if (!wxMessage.getMsgType().equals("event")) {}
        final String content = "";
        final String snArr = wxMessage.getContent();
        return (WxMpXmlOutMessage)((TextBuilder)((TextBuilder)WxMpXmlOutMessage.TEXT().content(snArr).fromUser(wxMessage.getToUser())).toUser(wxMessage.getFromUser())).build();
    }
    
    public String getMsg(final String msgName, final Object... values) {
        final List<Dict> textList = this.dictService.selectByTypeCode("text_list");
        final Map<String, Dict> textMap = textList.stream().collect(Collectors.toMap(dict -> dict.getName(), dict -> dict));
        final Dict textDict = textMap.get(msgName);
        final String msgTemplate = textDict.getCode();
        return parseMsgFormat(msgTemplate, values);
    }
    
    public static String parseMsgFormat(final String msgTemplate, final Object... values) {
        String msg = "";
        if (StringUtils.isNotBlank((CharSequence)msgTemplate)) {
            String format = msgTemplate.replaceAll("\\{[^}]*\\}", "{}");
            final int groupCount = StringUtils.countMatches((CharSequence)format, (CharSequence)"{}");
            final String[] groups = format.split("\\{\\}");
            String newFormat = "";
            int index = 0;
            for (final String group : groups) {
                newFormat = newFormat + group + "{" + index++ + "}";
            }
            newFormat = StringUtils.removeEnd(newFormat, "{" + groupCount + "}");
            final Pattern pattern = Pattern.compile("<\\?([^{]*\\{(\\d+)\\}[^?]*)\\?>");
            final Matcher matcher = pattern.matcher(newFormat);
            final Map<String, String> replaceMap = new HashMap<String, String>();
            while (matcher.find()) {
                final String groupIndex = matcher.group(2);
                final int groupIndexInt = NumberUtils.toInt(groupIndex, -1);
                if (groupIndexInt > -1 && groupIndexInt < values.length) {
                    final Object value = values[groupIndexInt];
                    final String group2 = matcher.group(0).replaceAll("\\{\\d+\\}", "{}");
                    final String group3 = matcher.group(1).replaceAll("\\{\\d+\\}", "{}");
                    if (value instanceof Boolean) {
                        if (!(boolean)value) {
                            replaceMap.put(group2, "");
                        }
                        else {
                            replaceMap.put(group2, group3.replace("{}", ""));
                        }
                    }
                    else if (!ObjectUtils.isEmpty(value)) {
                        replaceMap.put(group2, group3);
                    }
                    else {
                        replaceMap.put(group2, "{}");
                    }
                    for (final String content : replaceMap.keySet()) {
                        format = format.replace(content, replaceMap.get(content));
                    }
                }
            }
            System.out.println(format);
            msg = StrUtil.format((CharSequence)format, values);
        }
        else {
            msg = StringUtils.join((Object[])new String[] { values + "," });
        }
        return msg;
    }
    
    public static void main(final String[] args) {
        final String msg = "\u5df2\u5207\u6362\u4e3a\u67e5\u300a{\u67e5\u8be2\u7c7b\u522b}\u300b\uff1a\n\u8bf7\u8f93\u5165\u5e8f\u5217\u53f7\u6216IMEI\n\u6279\u91cf\u4e00\u6b21\u6700\u591a20\u6761\n\u8bf7\u7528\u7a7a\u683c\u6216\u6362\u884c\u9694\u5f00\n\u5305\u6708\u5957\u9910,{\u5305\u6708\u5269\u4f59}\n<?\u5269\u4f59\u6b21\u6570\u4e0d\u8db3,\u7ee7\u7eed\u67e5\u8be2\u5c06\u81ea\u52a8\u6263\u9664\u4f59\u989d.{\u5360\u4f4d\u7b26}?>";
        final String split = StringUtils.repeat("*", 33);
        System.out.println(parseMsgFormat(msg, "\u67e5\u4fdd\u4fee", 1, true));
    }
}
